from fastapi import FastAPI
import uvicorn
from dotenv import load_dotenv
import os

# 加载环境变量
load_dotenv()

# 导入路由模块
from app.routers import items, users, system

# 创建FastAPI实例
app = FastAPI(
    title="FastAPI Starter API",
    description="一个简单的FastAPI示例项目，所有API路径都以/api开头",
    version="0.1.0",
    docs_url="/api/docs",
    redoc_url="/api/redoc",
    openapi_url="/api/openapi.json"
)

# 注册路由
app.include_router(items.router)
app.include_router(users.router)
app.include_router(system.router)

# 根路径重定向到API文档
@app.get("/", include_in_schema=False)
async def root():
    return {"message": "欢迎访问FastAPI Starter API，请访问 /api/docs 查看API文档"}

# 启动服务器
if __name__ == "__main__":
    host = os.getenv("API_HOST", "0.0.0.0")
    port = int(os.getenv("API_PORT", 8070))
    log_level = os.getenv("LOG_LEVEL", "info")
    
    print(f"启动服务器: http://{host if host != '0.0.0.0' else 'localhost'}:{port}")
    print(f"API文档地址: http://{host if host != '0.0.0.0' else 'localhost'}:{port}/api/docs")
    
    uvicorn.run("main:app", host=host, port=port, reload=True, log_level=log_level)